//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml;

namespace OpenLS.Spreadsheet.UI
{
    public class ForcedCulture
    {
        public static void StoreLanguage(string cultureName)
        {
            try
            {
                ForcedCulture i = new ForcedCulture();
                using (XmlWriter writer = new XmlTextWriter(FileName, Encoding.Default))
                {
                    i.CultureName = cultureName;
                    i.WriteXml(writer);
                }
            }
            catch { }
        }
        public static void ForceLanguage()
        {
            try
            {
                if (!File.Exists(FileName))
                    return;
                ForcedCulture i = new ForcedCulture();
                using (XmlReader reader = new XmlTextReader(FileName))
                {
                    i.ReadXml(reader);
                    if (i.CultureName != null)
                    {
                        Thread.CurrentThread.CurrentCulture = new CultureInfo(i.CultureName);
                        Thread.CurrentThread.CurrentUICulture = new CultureInfo(i.CultureName);
                    }
                }
            }
            catch { }
        }
        string CultureName;
        static string FileName
        {
            get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "language.xml"); }
        }

        public void ReadXml(XmlReader reader)
        {
            reader.Read();
            CultureName = reader["CultureName"];
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("Language", "foo");
            if (CultureName != null)
            {
                writer.WriteStartAttribute("CultureName");
                writer.WriteString(CultureName);
                writer.WriteEndAttribute();
            }
            writer.WriteEndElement();
        }



        
    }
}
